package com.wxy.xr.dao.impl;

import com.wxy.xr.bean.Contact;
import com.wxy.xr.bean.ContactListParam;
import com.wxy.xr.bean.ContactListResult;
import com.wxy.xr.dao.ContactDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import java.util.ArrayList;
import java.util.List;


public class ContactDaoImpl extends BaseDaoImpl<Contact> implements ContactDao {


    @Override
    public boolean save(Contact bean) {
        Integer id=bean.getId();
        List<Object> args = new ArrayList<>();
        args.add(bean.getName());
        args.add(bean.getEmail());
        args.add(bean.getComment());
        args.add(bean.getSubject());
        args.add(bean.getAlreadyRead());
        String sql;
        if(id==null || id<1){
            sql="INSERT INTO contact(name,email,comment,subject,already_read) values(?,?,?,?,?)";
        } else {
            sql="UPDATE award set name=? ,email =? ,comment=?, subject=?, already_read=? where id =?";
            args.add(id);
        }
        return tpl.update(sql,args.toArray())>0;
    }

    @Override
    public Contact get(Integer id) {
        String sql="select id, created_time, name,email,comment,subject,already_read from contact where id=?";
        return tpl.queryForObject(sql,new BeanPropertyRowMapper<>(Contact.class),id);
    }

    @Override
    public List<Contact> list() {
        String sql="select id,created_time,  name,email,comment,subject,already_read from contact" ;
        return tpl.query(sql,new BeanPropertyRowMapper<>(Contact.class));
    }

    @Override
    public ContactListResult list(ContactListParam param) {
        ContactListResult result=new ContactListResult();
        StringBuilder sql=new StringBuilder();
        sql.append("select id,created_time,name,email,comment,subject,already_read from contact where 1=1 ");
        List<Object> args = new ArrayList<>();

        StringBuilder condition = new StringBuilder();
        if(param.getBeginDay()!=null){
            condition.append("and created_time>=? ");
            args.add(param.getBeginDay());
            result.setBeginDay(param.getBeginDay());
        }
        if(param.getEndDay()!=null){
            condition.append("and created_time<=? ");
            args.add(param.getEndDay());
            result.setEndDay(param.getEndDay());
        }
        String keyword = param.getKeyword();
        if(keyword!=null && keyword.length()>0){
            condition.append("and (name like ? or email like ? or subject like ? or comment like ? ) ");
            keyword="%"+keyword+"%";
            args.add(keyword);
            args.add(keyword);
            args.add(keyword);
            args.add(keyword);
            result.setKeyword(param.getKeyword());
        }
        Integer read=param.getAlreadyRead();
        if(read!=null && read<ContactListParam.READ_ALL){
            condition.append("and already_read=? ");
            args.add(read);
            result.setAlreadyRead(read);
        }


        Integer pageSize=param.getPageSize();
        if(pageSize==null){
            pageSize=10;
        }


        String countSql="select count(*) from contact where 1=1 "+condition;

        Integer totalCount=tpl.queryForObject(countSql, Integer.class, args.toArray());
        if(totalCount == 0) return result;
        result.setTotalPages((totalCount+pageSize-1)/pageSize);
        result.setTotalCount(totalCount);


        sql.append(condition);
        sql.append("limit ?,?");
        Integer pageNo=param.getPageNo();

        if(pageNo==null){
            pageNo=1;
        }else if(pageNo>result.getTotalPages()){
            pageNo=result.getTotalPages();
        }



        args.add((pageNo-1)*pageSize);
        args.add(pageSize);
        result.setPageNo(pageNo);
        result.setPageSize(pageSize);
        List<Contact> contacts= tpl.query(sql.toString(),new BeanPropertyRowMapper<>(Contact.class),args.toArray());
        result.setContacts(contacts);
        return result;
    }

    @Override
    public boolean read(Integer id) {
        String sql="update contact set already_read=1 where id = ? ";
        return tpl.update(sql,id)>0;
    }
}
